package is.glif.fenius.client.connection;

import is.glif.fenius.client.connection.beans.YAMLListReqList;
import is.glif.fenius.client.connection.beans.YAMLQueryReqList;
import is.glif.fenius.client.connection.beans.YAMLReleaseReqList;
import is.glif.fenius.client.connection.beans.YAMLResvList;
import is.glif.fenius.client.connection.beans.YAMLServerInfo;
import is.glif.fenius.util.ConfigManager;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Map;

import org.apache.log4j.Logger;
import org.ho.yaml.Yaml;

public class ConfigHolder {
    private final static Logger log = Logger.getLogger(ConfigHolder.class);
    
    private static ConfigHolder instance;
    private String serversConfigFile;
    private String resvsConfigFile;
    private String queriesConfigFile;
    private String listsConfigFile;
    private String releasesConfigFile;
    public String getReleasesConfigFile() {
        return releasesConfigFile;
    }

    public void setReleasesConfigFile(String releasesConfigFile) {
        this.releasesConfigFile = releasesConfigFile;
    }

    private YAMLServerInfo servers;
    private YAMLResvList resvs;
    private YAMLListReqList listReqs;
    private YAMLReleaseReqList releaseReqs;
    private YAMLQueryReqList queryReqs;
    
    public YAMLReleaseReqList getReleaseReqs() {
        return releaseReqs;
    }

    public void setReleaseReqs(YAMLReleaseReqList releaseReqs) {
        this.releaseReqs = releaseReqs;
    }

    public static ConfigHolder getInstance() {
        if (instance == null) {
            instance = new ConfigHolder();
        }
        return instance;
    }
    
    private ConfigHolder() {
        ConfigManager cm = ConfigManager.getInstance();
        Map<String, Object> config = cm.getConfiguration();

        listsConfigFile = (String) config.get("listreqs");
        resvsConfigFile = (String) config.get("resvs");
        serversConfigFile = (String) config.get("servers");
        releasesConfigFile = (String) config.get("releases");
        queriesConfigFile = (String) config.get("queries");
        log.debug("lists: "+listsConfigFile);
        log.debug("releases: "+releasesConfigFile);
        log.debug("resvs: "+resvsConfigFile);
        log.debug("servers: "+serversConfigFile);
        log.debug("queries: "+queriesConfigFile);
    }
    
    
    public void loadConfig() {
        this.loadResvs();
        this.loadReleaseReqs();
        this.loadServers();
        this.loadQueryReqs();
        this.loadListReqs();
    }
    
    private void loadResvs() {
        log.debug("loading resvs from: "+resvsConfigFile);
        if (resvsConfigFile == null) return;
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(new File(resvsConfigFile));
            YAMLResvList yrl = (YAMLResvList) Yaml.loadType(fis, YAMLResvList.class);
            this.setResvs(yrl);
        } catch (FileNotFoundException e) {
            log.error(e);
            e.printStackTrace();
            return;
        }
    }
    private void loadServers() {
        log.debug("loading servers from "+serversConfigFile);
        if (serversConfigFile == null) return;
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(new File(serversConfigFile));
            YAMLServerInfo ysi = (YAMLServerInfo) Yaml.loadType(fis, YAMLServerInfo.class);
            this.setServers(ysi);
        } catch (FileNotFoundException e) {
            log.error(e);
            e.printStackTrace();
            return;
        }
    }

    private void loadReleaseReqs() {
        log.debug("loading release requests from: "+releasesConfigFile);
        if (releasesConfigFile == null) return;
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(new File(releasesConfigFile));
            YAMLReleaseReqList yrl = (YAMLReleaseReqList) Yaml.loadType(fis, YAMLReleaseReqList.class);
            this.setReleaseReqs(yrl);
        } catch (FileNotFoundException e) {
            log.error(e);
            e.printStackTrace();
            return;
        }
    }

    private void loadQueryReqs() {
        log.debug("loading query requests from: "+queriesConfigFile);
        if (queriesConfigFile == null) return;
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(new File(queriesConfigFile));
            YAMLQueryReqList yrl = (YAMLQueryReqList) Yaml.loadType(fis, YAMLQueryReqList.class);
            this.setQueryReqs(yrl);
        } catch (FileNotFoundException e) {
            log.error(e);
            e.printStackTrace();
            return;
        }
    }
    private void loadListReqs() {
        log.debug("loading list requests from: "+listsConfigFile);
        if (listsConfigFile == null) return;
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(new File(listsConfigFile));
            YAMLListReqList yrl = (YAMLListReqList) Yaml.loadType(fis, YAMLListReqList.class);
            this.setListReqs(yrl);
        } catch (FileNotFoundException e) {
            log.error(e);
            e.printStackTrace();
            return;
        }
    }
    
    
    public void setServersConfigFile(String serversConfigFile) {
        this.serversConfigFile = serversConfigFile;
    }
    public String getServersConfigFile() {
        return serversConfigFile;
    }
    public void setResvsConfigFile(String resvsConfigFile) {
        this.resvsConfigFile = resvsConfigFile;
    }
    public String getResvsConfigFile() {
        return resvsConfigFile;
    }

    public void setResvs(YAMLResvList resvs) {
        this.resvs = resvs;
    }

    public YAMLResvList getResvs() {
        return resvs;
    }

    public void setServers(YAMLServerInfo servers) {
        this.servers = servers;
    }

    public YAMLServerInfo getServers() {
        return servers;
    }

    public void setListsConfigFile(String listsConfigFile) {
        this.listsConfigFile = listsConfigFile;
    }

    public String getListsConfigFile() {
        return listsConfigFile;
    }

    public void setListReqs(YAMLListReqList listReqs) {
        this.listReqs = listReqs;
    }

    public YAMLListReqList getListReqs() {
        return listReqs;
    }

    public void setQueriesConfigFile(String queriesConfigFile) {
        this.queriesConfigFile = queriesConfigFile;
    }

    public String getQueriesConfigFile() {
        return queriesConfigFile;
    }

    public void setQueryReqs(YAMLQueryReqList queryReqs) {
        this.queryReqs = queryReqs;
    }

    public YAMLQueryReqList getQueryReqs() {
        return queryReqs;
    }
    
    
    
    
}
